package com.gu.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.exception.DiyException;
import com.gu.entity.EduComment;
import com.gu.entity.vo.front.CommentVo;
import com.gu.service.EduCommentService;
import entity.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * Description: 课程评论控件
 *
 * @Autor: Rolin
 * datetime: 06-18-21:57
 */
@RestController
@CrossOrigin
@Api(tags = "前台课程评论")
@RequestMapping("/eduService")
public class CommentController {

    @Resource
    private EduCommentService eduCommentService;

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @PostMapping("/front/addComment")
    @ApiOperation("前台添加评论")
    public CommonResult addComment(@RequestBody CommentVo commentVo) {
        boolean result = false;
        boolean result1 = !StringUtils.isEmpty(commentVo.getMemberId());
        boolean result2 = !StringUtils.isEmpty(commentVo.getTeacherId());
        boolean result3 = !StringUtils.isEmpty(commentVo.getCourseId());
        if (result1 && result2 && result3) {
            EduComment comment = new EduComment();
            BeanUtils.copyProperties(commentVo, comment);
            //将评论分别存放在 redis和mysql中(过期时间默认)
            redisTemplate.opsForValue().set(commentVo.getCourseId(), comment);
            result = eduCommentService.save(comment);
        } else {
            throw new DiyException("50000", "课程Id、会员Id和教师Id不能为空");
        }
        return result ? CommonResult.success() : CommonResult.error();
    }

    @GetMapping("/front/getCommentsByCourseId/{id}/{current}/{limit}")
    @ApiOperation("课程id获取所有评论,且分页")
    public CommonResult getCommentsByCourseId(@PathVariable("id") String courseId,
                                              @PathVariable("current") Long current,
                                              @PathVariable("limit") Long limit) {

        Page<EduComment> commentPage = new Page<EduComment>(current, limit);
        QueryWrapper<EduComment> eduCommentQueryWrapper = new QueryWrapper<>();
        eduCommentQueryWrapper.eq("course_id", courseId);
        eduCommentQueryWrapper.orderByDesc("gmt_create");
        Page<EduComment> page = eduCommentService.page(commentPage, eduCommentQueryWrapper);
        List<EduComment> commentList = page.getRecords();
        long totalPages = page.getPages();
        long totalRecords = page.getTotal();
        return CommonResult.success().data("totalPages", totalPages).data("totalRecords", totalRecords).data("commentList", commentList);
    }

    @DeleteMapping("/front/deleteCommentById/{id}")
    @ApiOperation("根据id来删除评论")
    public CommonResult deleteCommentById(@PathVariable("id") String id) {
        EduComment e = eduCommentService.getById(id);
        boolean result = false;
        String courseId = e.getCourseId();
        if (redisTemplate.delete(courseId)) {
            eduCommentService.removeById(id);
        }
        return result ? CommonResult.success() : CommonResult.error();
    }
}
